package sicnu.cs.ich.ich.ranking.rest;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sicnu.cs.ich.api.common.Constants;
import sicnu.cs.ich.api.common.ResultInfo;
import sicnu.cs.ich.api.domain.dto.IchHitDTO;
import sicnu.cs.ich.common.util.CollectionUtil;
import sicnu.cs.ich.common.util.ResultInfoUtil;
import sicnu.cs.ich.ich.ranking.service.IIchRankingService;

import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.stream.Collectors;

/**
 * @author CaiKe
 * @date 2021/12/2 13:37
 */
@Slf4j
@RestController
@RequestMapping("/ich")
@RequiredArgsConstructor
public class IchRankResource {
    private final IIchRankingService rankingService;

    /**
     * 获取非遗类别热度榜
     *
     * @return
     */
    @GetMapping("type/hit")
    public ResultInfo<int[]> getTypeHit() {
        log.info("type/hit");
        return ResultInfoUtil.buildSuccess(rankingService.getIchTypeHit());
    }

    /**
     * 从非遗热度榜中获取size个非遗的ichId
     */
    @GetMapping("hit")
    @Deprecated
    public List<Integer> getHitIchs(@RequestParam Integer size) throws InterruptedException {
        log.info("hit");
        if (size > Constants.MAX_ICH_COUNT) {
            throw new IllegalArgumentException("超过最大获取数：" + size);
        }
        return CollectionUtil.createRandomList(rankingService.getIchRankingIds(), size);
    }

    /**
     * 调用排序，同时获取非遗排行榜
     */
    @GetMapping("/sortAndGet")
    public List<Integer> sortAndGet() throws InterruptedException {
        return rankingService.getIchRankingIds();
    }

    /**
     * 后台调用手动排序
     */
    @GetMapping("hit/refresh")
    public ResultInfo<PriorityQueue<IchHitDTO>> sortAndReturn() {
        final PriorityQueue<IchHitDTO> ichHitDTOS = rankingService.sortAndGet();
        return ResultInfoUtil.buildSuccess(ichHitDTOS);
    }
}